package com.adtech.farmProducts.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.adtech.common.Common;
import com.adtech.common.http.AsyncHttpPostFormData;
import com.adtech.common.http.AsyncHttpResponse;
import com.adtech.common.http.AsyncOkHttpClient;
import com.adtech.common.http.AsyncResponseHandler;
import com.adtech.common.model.RspModel;
import com.adtech.common.widgets.load.LoadFactory;
import com.adtech.common.widgets.load.core.LoadManager;
import com.adtech.common.widgets.load.view.AbsView;
import com.adtech.farmProducts.App;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.base.BaseActivity;
import com.adtech.farmProducts.base.BaseNfcActivity;
import com.adtech.farmProducts.model.SourceInfo;
import com.adtech.farmProducts.widget.ClearTextIconEditText;
import com.adtech.farmProducts.widget.ErrorView;
import com.adtech.farmProducts.widget.LoadingView;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Time:2020/6/20
 * Created by xiaocz
 * Describe 开箱验货
 */
public class UpdateTagActivity extends BaseNfcActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.layout_base)
    LinearLayout layout_base;
    @BindView(R.id.edit_gs_name)
    TextView edit_gs_name;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_time)
    TextView tv_time;

    @BindView(R.id.tv_code)
    ClearTextIconEditText tv_code;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_unit)
    TextView tv_unit;
    @BindView(R.id.tv_weight)
    TextView tv_weight;

    private String code;


    private void showView(SourceInfo sourceInfo){
        tv_code.setText(sourceInfo.getSourceCode());
        tv_name.setText(sourceInfo.getProductName());
        tv_unit.setText(sourceInfo.getPackUnit());
        tv_address.setText(sourceInfo.getAddress());
        edit_gs_name.setText(sourceInfo.getCompanyName());
        tv_weight.setText(sourceInfo.getWeight()+sourceInfo.getUnit());
        tv_time.setText(sourceInfo.getHandleTime());
    }

    public static void show(Context context,String code) {
        Intent intent = new Intent(context, UpdateTagActivity.class);
        intent.putExtra("code",code);
        context.startActivity(intent);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        if(bundle!=null){
            code=bundle.getString("code");
        }
        return super.initArgs(bundle);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_update_tag;
    }

    @Override
    protected void initData() {
        tvTitle.setText("标签更换");
        if(code!=null && !"".equals(code)){
            tv_code.setText(code);
            searchInfo(code);
        }
        super.initData();
    }

    private void searchInfo(String searchValue) {
        loadingDialogShow(false);
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("sourceCode", searchValue);
        okHttpClient.posts(Common.Constant.API_IP + "handle/queryBySourceCode", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                onDismiss();
                layout_base.setVisibility(View.GONE);
                btn_submit.setVisibility(View.GONE);
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                onDismiss();
                Type type = new TypeToken<RspModel<SourceInfo>>() {
                }.getType();
                RspModel<SourceInfo> rspModel = getAnalysis(response, type, "溯源详情");
                if (rspModel == null) {
                    return;
                }
                switch (rspModel.getCode()) {
                    case 1:
                        if (rspModel.getData() != null) {
                            showView(rspModel.getData());
                            layout_base.setVisibility(View.VISIBLE);
                            btn_ok.setVisibility(View.GONE);
                            btn_submit.setVisibility(View.VISIBLE);
                        }else {
                            layout_base.setVisibility(View.GONE);
                            btn_submit.setVisibility(View.GONE);
                        }
                        break;
                    default:
                        layout_base.setVisibility(View.GONE);
                        btn_submit.setVisibility(View.GONE);
                       App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }

    @BindView(R.id.btn_ok)
    Button btn_ok;
    @BindView(R.id.btn_submit)
    Button btn_submit;

    @OnClick({R.id.btn_ok,R.id.btn_submit})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_ok:
                String valueCode = String.valueOf(tv_code.getText()).trim();
                if(!TextUtils.isEmpty(valueCode)){
                    hideKeyboard();
                    searchInfo(valueCode);
                }else {
                    App.getInstance().showMessage("请输入破损的溯源码!");
                }
                break;
            case R.id.btn_submit:
                String searchValue = String.valueOf(tv_code.getText()).trim();
                if(!TextUtils.isEmpty(searchValue)){
                    UpdateTag2Activity.show(mContext,searchValue);
                    finish();
                }else {
                    App.getInstance().showMessage("请输入破损的溯源码!");
                }
                break;
        }
    }
}
